﻿using System;
using System.Collections.Generic;
using Troschuetz.Random;

namespace CSharpHelpers.Randoms
{
    public class RandomHelper
    {
        /// <summary>
        /// 获取10个1位不重复数字随机列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetRandomList()
        {
            var numList = new List<string>();
            var rand = new System.Random();
            var count = 0;
            while (count != 10)
            {
                count = CountList(numList, rand);
            }

            return numList;
        }

        /// <summary>
        /// 统计一轮产生随机数的数量
        /// </summary>
        /// <param name="numList"></param>
        /// <param name="rand"></param>
        /// <returns></returns>
        private int CountList(List<string> numList, System.Random rand)
        {
            for (var i = 0; i < 10; i++)
            {
                var num = rand.Next(0, 10).ToString();
                if (!numList.Contains(num))
                {
                    numList.Add(num);
                }
            }

            return numList.Count;
        }

        /// <summary>
        /// 获取随机ID
        /// </summary>
        /// <returns></returns>
        public static string GetRandomID()
        {
            System.Random random = new System.Random(Environment.TickCount);
            string randomId = random.Next(10000000, 99999999).ToString();
            return randomId;
        }

        /// <summary>
        /// 随机生成CardPassword
        /// </summary>
        /// <returns></returns>
        public static int GetCardPassword()
        {
            System.Random random = new System.Random(Environment.TickCount);
            int cardPassword = random.Next(1, 16777215);

            return cardPassword;
        }

        private StandardGenerator m_StandardGenerator;
        public RandomHelper()
        {
            m_StandardGenerator = new StandardGenerator();
        }

        public int Next()
        {
            return m_StandardGenerator.Next();
        }

        public int Next(int maxValue)
        {
            return m_StandardGenerator.Next(maxValue + 1);
        }

        public int Next(int minValue, int maxValue)
        {
            return m_StandardGenerator.Next(minValue, maxValue + 1);
        }

        /// <summary>
        /// 返回一个大于等于 minValue 且小于maxValue 的 32 位带符号整数
        /// 返回的值不包含maxValue值
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public static int GetRandom32(int minValue, int maxValue)
        {
            if (minValue > maxValue)
            {
                throw new ArgumentOutOfRangeException("maxValue", maxValue, "minValue > maxValue");
            }

            System.Random rd = NewRandom();
            int rdNum = rd.Next(minValue, maxValue);

            return rdNum;
        }


        public static System.Random NewRandom()
        {
            var rm = new System.Random(RNGProvider.Chaos_GetRandomSeed());
            return rm;
        }
    }
}
